package com.naiterui.ehp.bs.cms.modules.base.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.cms.common.constants.DsName;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.base.entity.Department;
import com.naiterui.ehp.bs.cms.modules.base.mapper.DepartmentBaseMapper;
import com.naiterui.ehp.bs.cms.modules.base.service.IDepartmentService;
import com.naiterui.ehp.bs.cms.modules.base.vo.DepartmentPageVO;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 潘xin
 * @since 2021/9/10
 */
@Slf4j
@Service
@DS(DsName.EHP)
public class DepartmentServiceImpl extends ServiceImpl<DepartmentBaseMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentBaseMapper departmentMapper;

    @Override
    public PageVO<DepartmentPageVO> queryPage(PageParamsVO pageParamsVO, DepartmentPageVO departmentVO) {

        QueryWrapper<DepartmentPageVO> entityWrapper = this.buildEntityWrapper(pageParamsVO, departmentVO);
        log.info("查询参数departmentVO={}", departmentVO.toString());
        Page<DepartmentPageVO> page = baseMapper.getPage(pageParamsVO.getPage(), entityWrapper);

        return PageVO.toPageVo(page);
    }

    @Override
    public void saveOrUpdate(DepartmentPageVO departmentPageVO) {
        Department department = new Department();
        BeanUtil.copyProperties(departmentPageVO, department);
        // 根据id判断保存和修改 departmentId
        if (department.getId() != null) {
            baseMapper.updateById(department);
        } else {
            baseMapper.insert(department);
        }
    }

    @Override
    public void updateDepartmentStatus(Long department, Integer status) {
        departmentMapper.updateDepartmentStatus(department, status);
    }

    private QueryWrapper<DepartmentPageVO> buildEntityWrapper(PageParamsVO pageParamsVO,
        DepartmentPageVO departmentVO) {
        QueryWrapper<DepartmentPageVO> entityWrapper = pageParamsVO.getEntityWrapper("t");
        Integer id = departmentVO.getId();
        entityWrapper.eq(id != null, "t.id", id);
        Boolean available = departmentVO.getAvailable();
        entityWrapper.eq(available != null && available == false, "t.available", 1);
        entityWrapper.eq(available != null && available == true, "t.available", 0);
        Integer parentId = departmentVO.getParentId();
        entityWrapper.eq(parentId != null && parentId != 0, "t.parent_id", parentId == -1 ? 0 : parentId);
        String code = departmentVO.getCode();
        entityWrapper.like(StringUtils.isNotBlank(code), "t.code", code);
        String name = departmentVO.getName();
        entityWrapper.like(StringUtils.isNotBlank(name), "t.name", name);
        return entityWrapper;
    }

}
